{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 提取对角线元素\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "def extract_main_diagonal(matrix):\n",
    "    rows = len(matrix)\n",
    "    cols = len(matrix[0])\n",
    "    size = min(rows, cols)\n",
    "    diagonal = [matrix[i][i] for i in range(size)]\n",
    "    return diagonal"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:12:43.790381Z",
     "end_time": "2024-07-03T16:12:43.795165Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 5]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix = [[1, 2, 3],\n",
    "          [4, 5, 6]]\n",
    "main_diagonal = extract_main_diagonal(matrix)\n",
    "main_diagonal"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:13:00.538611Z",
     "end_time": "2024-07-03T16:13:00.547332Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
